package activity.addCamera;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Paint;
import android.graphics.drawable.ColorDrawable;
import android.location.LocationManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.hichip.campro.R;

import base.HiActivity;
import butterknife.BindView;
import common.A2bigA;
import common.HiDataValue;
import common.TitleView;
import custom.dialog.NiftyDialogBuilder;
import main.MainActivity;
import utils.EmojiFilter;
import utils.FullCharFilter;
import utils.HiToast;
import utils.HiTools;
import utils.NotCopyAndPaste;

/**
 * 开始声波配置WiFi 界面
 *
 * @author lt
 */
public class SoundWavesWiFiOneKeySetActivity extends HiActivity implements OnClickListener {
    private EditText wifi_ssid_et;
    private EditText psw_wifi_et;
    private TextView tv_start_config;
    private String ssid;
    @BindView(R.id.iv_to_choice) ImageView iv_to_choice;
    @BindView(R.id.tv_uid) TextView tv_uid;
    @BindView(R.id.tv_hint) TextView tv_hint;
    @BindView(R.id.ll_confirm_uid) LinearLayout ll_confirm_uid;

    private String uid;
    private boolean mIsReset = false;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_wifi_onekeyset;
    }

    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        getIntentData();
        initViewAndData();
        setListeners();

    }

    private void getIntentData() {
        uid = getIntent().getStringExtra(HiDataValue.EXTRAS_KEY_UID);
        mIsReset = getIntent().getBooleanExtra("reset", false);
    }

    private void setListeners() {
        iv_to_choice.setOnClickListener(this);
        //iv_scan.setOnClickListener(this);
        tv_hint.setOnClickListener(this);
    }

    private String getInfo() {
        WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifi.getConnectionInfo();

        ssid = info.getSSID();
        if (ssid != null && ssid.length() > 1) {
            ssid = ssid.substring(1, ssid.length() - 1);
        }
        return ssid;
    }

    private void initViewAndData() {
        TitleView title_top = (TitleView) findViewById(R.id.title_top);
        title_top.setTitle(getString(R.string.title_config_wifi));
        title_top.setButton(TitleView.NAVIGATION_BUTTON_LEFT);
        title_top.setRightTxtBack(R.mipmap.x_bule);
        title_top.setNavigationBarButtonListener(new TitleView.NavigationBarButtonListener() {
            @Override
            public void OnNavigationButtonClick(int which) {
                switch (which) {
                    case TitleView.NAVIGATION_BUTTON_LEFT:
                        finish();
                        break;
                    case TitleView.NAVIGATION_BUTTON_IV_RIGHT:
                        Intent intent = new Intent(SoundWavesWiFiOneKeySetActivity.this, MainActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent, true);
                        break;
                }
            }
        });

        tv_hint.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        tv_hint.getPaint().setAntiAlias(true);

        //UID 编辑框自动转换为大写
        tv_uid.setTransformationMethod(new A2bigA());
        if (!TextUtils.isEmpty(uid)) {
            tv_uid.setText(uid);
        }

        if (TextUtils.isEmpty(uid)) {
            ll_confirm_uid.setVisibility(View.GONE);
        } else {
            // tv_uid.setEnabled(false);
        }

        wifi_ssid_et = (EditText) findViewById(R.id.wifi_ssid_et);
        psw_wifi_et = (EditText) findViewById(R.id.psw_wifi_et);
        psw_wifi_et.setFilters(new InputFilter[]{new InputFilter.LengthFilter(63), new FullCharFilter(this), new EmojiFilter()});
        psw_wifi_et.setCustomSelectionActionModeCallback(new NotCopyAndPaste());
        tv_start_config = findViewById(R.id.tv_start_config);
        if (mIsReset) {
            //iv_scan.setVisibility(View.GONE);
            //tv_uid.setEnabled(false);
        }


        final String uuid = getInfo();

        if (uuid != null) {
            wifi_ssid_et.setText(uuid);
        }

        psw_wifi_et.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        tv_start_config.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String uid = tv_uid.getText().toString().toUpperCase();
                if (!TextUtils.isEmpty(uid)) {
                    if (!HiTools.checkIsUid(uid)) {
                        HiToast.showToast(SoundWavesWiFiOneKeySetActivity.this, getString(R.string.input_uid_format_error));
                        return;
                    }
                }
                String psw = psw_wifi_et.getText().toString();
                if (HiTools.isMaxLength(ssid, 32)) {
                    HiToast.showToast(SoundWavesWiFiOneKeySetActivity.this, getString(R.string.toast_ssid_tolong));
                    return;
                }
                if (!ssid.equals(ssid.trim()) || TextUtils.isEmpty(psw) || (!psw.equals(psw.trim()))) {
                    HiToast.showToast(SoundWavesWiFiOneKeySetActivity.this, getString(R.string.toast_no_space));
                    return;
                }
                View customView = View.inflate(SoundWavesWiFiOneKeySetActivity.this, R.layout.pup_phone_near_device, null);
                PopupWindow popupWindow = new PopupWindow(customView);
                ColorDrawable cd = new ColorDrawable(-000);
                popupWindow.setBackgroundDrawable(cd);
                popupWindow.setFocusable(true);
                popupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
                popupWindow.setHeight(WindowManager.LayoutParams.MATCH_PARENT);
                popupWindow.showAtLocation(customView, Gravity.CENTER, 0, 0);
                TextView tv_ready = customView.findViewById(R.id.tv_ready);
                tv_ready.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popupWindow.dismiss();
                        String input_uid = tv_uid.getText().toString().toUpperCase();
                        Intent intent = new Intent(SoundWavesWiFiOneKeySetActivity.this, SoundWavesConfigActivity.class);
                        intent.putExtra(HiDataValue.EXTRAS_KEY_UID, input_uid);
                        String ssid = wifi_ssid_et.getText().toString();
                        String input_psw = psw_wifi_et.getText().toString();
                        intent.putExtra("ssid", ssid);
                        intent.putExtra("psw", input_psw);
                        intent.putExtra("reset", mIsReset);
                        startActivity(intent);
                    }
                });


            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_to_choice:
                LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
                boolean ok = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
                if (ok) {
                    Intent intent = new Intent(SoundWavesWiFiOneKeySetActivity.this, PhoneWiFiListActivity.class);
                    startActivityForResult(intent, 110);
                } else {
                    AlertDialog.Builder dialog = new AlertDialog.Builder(this);
                    dialog.setTitle(getString(R.string.tip_reminder)).setMessage(getString(R.string.open_location));
                    dialog.setPositiveButton(getString(R.string.go_to_open), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent = new Intent();
                            intent.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                            startActivityForResult(intent, 1315);
                        }
                    });
                    dialog.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                        }
                    });
                    dialog.show();
                }
                break;
            case R.id.tv_hint://疑问解答
                View customView = View.inflate(SoundWavesWiFiOneKeySetActivity.this, R.layout.pup_wifi_require, null);
                PopupWindow popupWindow = new PopupWindow(customView);
                ColorDrawable cd = new ColorDrawable(-000);
                popupWindow.setBackgroundDrawable(cd);
                popupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
                popupWindow.setHeight(WindowManager.LayoutParams.MATCH_PARENT);
                popupWindow.setFocusable(true);
                popupWindow.showAtLocation(customView, Gravity.CENTER, 0, 0);
                TextView tv_know = customView.findViewById(R.id.tv_know);
                tv_know.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popupWindow.dismiss();
                    }
                });
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 110:
                if (resultCode == RESULT_OK) {
                    String ssid = data.getStringExtra("configssid");
                    if (!TextUtils.isEmpty(ssid)) {
                        wifi_ssid_et.setText(ssid);
                    }
                }
                break;
            case 119:
                if (resultCode == RESULT_OK) {
                    Bundle bundle = data.getExtras();
                    String uid = bundle.getString(HiDataValue.EXTRAS_KEY_UID, "");
                    if (!TextUtils.isEmpty(uid)) {
                        tv_uid.setText(uid);
                    }
                }
                break;
            case 1315:
                LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
                boolean ok = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
                if (ok) {
                    Intent intent = new Intent(SoundWavesWiFiOneKeySetActivity.this, PhoneWiFiListActivity.class);
                    startActivityForResult(intent, 110);
                }
                break;
        }
    }

}
